import {
	cookie
} from '../../utils/cookie';
const baseURL = 'http://api_cloud.vilionsmart.com';


function getHeader() {
	// 获取 token
	const token = cookie.get('token');
	const Language = cookie.get('applicationLocale')
	const nodeVal = cookie.get('nodeVal')
	return {
		'Accept-Language': Language ? Language : '',
		"lookupKey": nodeVal ? nodeVal : '',
		token: token ? token : ''
	}
}

function upload(url, filePath) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${baseURL}${url}`,
			filePath: filePath,
			name: 'file', 
			header: getHeader(),
			success(res) {
				try{
					const { code, msg, data } =  JSON.parse(res.data);
					if (code == 200) {
						resolve(data);
					}else {
						reject(data || msg)
					}
				}catch(err){
					
					reject(err.message)
				}
			},
			fail(e) {
				reject()
			}
		})
	})
}

export function uploadFile(filePath) {
	return upload('/vilion/system/uploadFile', filePath);
}

export function uploadAvatar(filePath) {
	return upload('/vilion/system/uploadAvatar', filePath);
}